import type { Metadata } from "next";
import Nav from "@/base-framework/landing/Nav";
import Hero from "@/base-framework/landing/Hero";
import What from "@/base-framework/landing/What";
import How from "@/base-framework/landing/How";
import Features from "@/base-framework/landing/Features";
import FAQ from "@/base-framework/landing/FAQ";
import Footer from "@/base-framework/landing/Footer";
import type { LandingData, JsonLandingData } from "@/base-framework/types/landing";

// 动态导入所有配置文件
import seoData from "@/base-framework/content/landing/seo.json" assert { type: "json" };
import heroData from "@/base-framework/content/landing/hero.json" assert { type: "json" };
import whatData from "@/base-framework/content/landing/what.json" assert { type: "json" };
import howData from "@/base-framework/content/landing/how.json" assert { type: "json" };
import featuresData from "@/base-framework/content/landing/features.json" assert { type: "json" };
import faqData from "@/base-framework/content/landing/faq.json" assert { type: "json" };
import footerData from "@/base-framework/content/landing/footer.json" assert { type: "json" };

const jsonData: JsonLandingData = {
  seo: seoData,
  hero: heroData,
  what: whatData,
  how: howData,
  features: featuresData,
  faq: faqData,
  footer: footerData
};

const landing = jsonData as LandingData;

export function generateMetadata(): Metadata {
  const s = landing.seo;
  return {
    title: s.title,
    description: s.description,
    alternates: s.canonical ? { canonical: s.canonical } : undefined,
    keywords: s.keywords,
    openGraph: s.openGraph
      ? {
          title: s.openGraph.title ?? s.title,
          description: s.openGraph.description ?? s.description,
          images: s.openGraph.image ? [{ url: s.openGraph.image }] : undefined,
        }
      : undefined,
    twitter: s.twitter
      ? {
          card: s.twitter.card as "summary" | "summary_large_image",
          title: s.twitter.title ?? s.title,
          description: s.twitter.description ?? s.description,
          images: s.twitter.image ? [s.twitter.image] : undefined,
        }
      : undefined,
  };
}

function StructuredData() {
  const faq = landing.seo.structuredData?.faq;
  if (!faq?.length) return null;
  const jsonLd = {
    "@context": "https://schema.org",
    "@type": "FAQPage",
    mainEntity: faq.map((f) => ({
      "@type": "Question",
      name: f.q,
      acceptedAnswer: { "@type": "Answer", text: f.a },
    })),
  } as const;

  return (
    <script
      type="application/ld+json"
      dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }}
    />
  );
}

export default function LandingPage() {
  return (
    <div className="min-h-screen">
      <Nav />
      <main id="main">
        <Hero data={landing.hero} />
        {landing.what && <What items={landing.what.items} />}
        {landing.how && <How steps={landing.how.steps} />}
        {landing.features && <Features groups={landing.features.groups} />}
        {landing.faq && <FAQ items={landing.faq.items} />}
      </main>
      <Footer data={landing.footer} />
      <StructuredData />
    </div>
  );
}